import fs from 'fs'
import path from 'path'

const ROOT_DIR = path.join(path.resolve(), '/doc/')

// 递归读取文件夹
export default function (dir) {
  async function readFolders(folder) {
    // console.log('folder', folder)
    const pages = []
    const files = await fs.readdirSync(folder, { withFileTypes: true })
    if (files.length === 0) return

    files.forEach((file) => {
      if (file.isDirectory()) {
        // const newFolder = path.join(dir, file.name)
        // console.log(folder, dir, file.name)
        readFolders(path.resolve(folder, file.name))
      } else {
        pages.push(file.name)
      }
    })

    return pages
  }

  return readFolders(path.resolve(ROOT_DIR, dir))
}
